package JavaSE.CODE.ClassInCommonUse.PackagingClass;

public class test {

    public static void main(String[] args) {
        String s = "IV";
        System.out.println(romanToInt(s));
    }

    public static int romanToInt(String s) {
        int sum=0;
        char[] chars = s.toCharArray();
        for(int i=0;i<chars.length;i++){
            if(chars[i]=='I'&&i<chars.length-1){
                if(chars[i+1]=='V'){
                    sum+=4;
                    i++;
                }
                else if(chars[i+1]=='X'){
                    sum+=9;
                    i++;
                }
                sum+=1;
            }
            else if(chars[i]=='X'&&i<chars.length-1){
                if(chars[i+1]=='L'){
                    sum+=40;
                    i++;
                }
                else if(chars[i+1]=='C'){
                    sum+=90;
                    i++;
                }
                sum+=10;
            }
            else if(chars[i]=='C'&&i<chars.length-1){
                if(chars[i+1]=='D'){
                    sum+=400;
                    i++;
                }
                else if(chars[i+1]=='M'){
                    sum+=900;
                    i++;
                }
                sum+=100;
            }
            else{
                if(chars[i]=='M'){
                    sum+=1000;
                }
                else if(chars[i]=='D'){
                    sum+=500;
                }
                else if(chars[i]=='C'){
                    sum+=100;
                }
                else if(chars[i]=='L'){
                    sum+=50;
                }
                else if(chars[i]=='X'){
                    sum+=10;
                }
                else if(chars[i]=='V'){
                    sum+=5;
                }
                else{
                    sum+=1;
                }
            }


        }
        return sum;
    }
}
